//
//  WPProductViewController.m
//  MyLottery
//
//  Created by wpapple on 16/5/27.
//  Copyright © 2016年 wpapple. All rights reserved.
//

#import "WPProductViewController.h"
#import "WPproduct.h"
#import "ProductViewCell.h"

@interface WPProductViewController ()

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation WPProductViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 120);
    
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    return [super initWithCollectionViewLayout:flowLayout];
}

#pragma mark - 懒加载
-(NSArray *)dataArray
{
    if (!_dataArray) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"product.json" ofType:nil];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray * tempArray = [NSMutableArray arrayWithCapacity:array.count];
        
        for (NSDictionary *dict in array) {
            WPproduct *product = [WPproduct productWithDict:dict];
            
            [tempArray addObject:product];
        }
        
        _dataArray = [tempArray copy];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
//    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    UINib *nib = [UINib nibWithNibName:@"ProductViewCell" bundle:nil];
    
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:reuseIdentifier];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (ProductViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ProductViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    WPproduct *product = self.dataArray[indexPath.row];
    
    cell.appImageView.image = [UIImage imageNamed:product.icon];
    cell.appLabelView.text = product.title;
    
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPproduct *product = self.dataArray[indexPath.row];
    
    UIApplication *application = [UIApplication sharedApplication];
    
    NSURL *url = [NSURL URLWithString:product.url];
    
    BOOL canOpen = [application canOpenURL:url];
    
    if (canOpen) {
        [application openURL:url];
    }else
    {
        NSLog(@"errer");
    }
}
@end
